﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BrewXML.Core.Data
{
    public class RECIPE
    {
        #region NAME

        public string NAME { get; set; }

        #endregion

        #region VERSION

        public string VERSION { get; set; }

        #endregion

        #region BREWER

        public string BREWER { get; set; }

        #endregion

        #region STYLE

        private STYLE mSTYLE;

        public STYLE STYLE
        {
            get
            {
                return mSTYLE;
            }
            set
            {
                mSTYLE = value;
            }
        }

        #endregion

        #region BATCH_SIZE

        public double BATCH_SIZE { get; set; }

        #endregion

        #region OG

       
        public double OG
        {
            get
            {
                if(GRAINS!=null)
                return CalOG();
                else
                {
                    return 0;
                }
               
            }
            
        }

        #endregion

        #region FG

        public double FG
        {
            get
            {
                return CalFG();
            }
        }

        #endregion

        #region IBU

       
        public int IBU
        {
            get
            {
                if(HOPS!=null)
                return CalIBU();
                else
                {
                    return 0;
                }
            }
           
        }

        #endregion

        #region COLOR

        
        public int COLOR
        {
            get
            {
                if (GRAINS != null)
                    return CalCOLOR();
                else
                {
                    return 0;
                }
            }
          
        }

        #endregion

        #region ABV

        private double mABV ;
        public double ABV
        {
            get
            {
                double tmp = ((OG - FG)*1.29)*100;
                return Math.Round(tmp,1);
            }
        }

        #endregion

        #region ABW

        private double mABW;
        public double ABW
        {
            get
            {
                //ABW=ABV*0.785
                double tmp = ((OG - FG) * 1.29) * 0.785 * 100;
                return Math.Round(tmp, 1);
                
            }
        }

        #endregion

        #region GRAINS
        private List<GRAIN> mGRAINS=new List<GRAIN>();

        public List<GRAIN> GRAINS
        {
            get
            {
                return mGRAINS;
            }
            set
            {
                mGRAINS = value;
                
                
            }
        }

        #endregion

        #region HOPS
        private List<HOP> mHOPS = new List<HOP>();

        public List<HOP> HOPS
        {
            get
            {
                return mHOPS;
            }
            set
            {
                mHOPS = value;
                
            }
        }

        #endregion


        #region MISCS
        private List<MISC> mMISCS = new List<MISC>();

        public List<MISC> MISCS
        {
            get
            {
                return mMISCS;
            }
            set
            {
                mMISCS = value;
                
            }
        }

        #endregion

        #region Constructors

        public RECIPE()
        {
            NAME = "New Recipe";
            VERSION = "1";
            BREWER = "Brewer Name";
            
        }
        #endregion

        #region Function

        #region CalOG
        public double CalOG()
        {
            double yield;
            double est = 0.0;
            foreach (var item in GRAINS)
            {
                yield = item.YIELD;
                if (item.USE == "Mashed") yield *= 0.75;
                else
                {
                    if (item.USE == "Steeped") yield *= 0.5;
                }
                est += yield;
            }
            if (BATCH_SIZE != 0)
            {
                est /= BATCH_SIZE;
            }
            else
            {
                est = 0.0;
            }
            double tmp = est + 1.0;
            return Math.Round(tmp, 3);
        }
        #endregion

        #region CalFG
        public double CalFG()
        {
            if (OG <= 0.0) return 0.0;
            double tmp = (((OG - 1.0)*0.25) + 1.0);
            return Math.Round(tmp, 3); ;
           
        }
        #endregion

        #region CalIBU
        public int CalIBU()
        {
            double bitterness = 0.0;
            foreach (var item in HOPS)
            {
                bitterness += item.HBU * item.TIME;
            }
            if (BATCH_SIZE != 0)
            {
                bitterness /= BATCH_SIZE;
            }
            else
            {
                bitterness = 0.0;
            }
            if (OG > 1.050) bitterness /= 1.0 + ((OG - 1.050) / 0.2);
            return (int)Math.Round(bitterness, 0);
        }
        #endregion

        #region CalCOLOR
        public int CalCOLOR()
        {
            double srm = 0.0;
            foreach (var item in GRAINS)
            {
                srm += item.HCU;
            }
            if (BATCH_SIZE != 0)
            {
                srm /= BATCH_SIZE;
            }
            else
            {
                srm = 0.0;
            }

            srm = Math.Pow(srm, 0.6859) * 1.4922;
            if (srm > 50) srm = 50;
            return (int)Math.Round(srm, 0);
        }
        #endregion

        #region

       
        #endregion

        #endregion
    }
}
